﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using App.Core.Extensions;
using App.Data;
using App.Core.DB;
using System.Web.Services;

public partial class Pages_Public_EventList : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindEventList(1);
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    protected void BindEventList(int pageNumber)
    {
        UtilityDAO dao = new UtilityDAO();
        int totalRecord = 0;
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_ALL_EVENTS, null, pageNumber, ConfigReader.EventBookingListSize, out totalRecord);
        rptEventList.DataSource = ds.Tables[0];
        rptEventList.DataBind();

        ///Bind the Pager Control
        ucEventListPager.TotalRecord = totalRecord;
        ucEventListPager.PageNo = pageNumber;
        ucEventListPager.PageSize = ConfigReader.EventBookingListSize;
        ucEventListPager.TotalRecordMessage = "Total {0} Events Found.";
        ucEventListPager.DataBind();

    }

    protected void ucEventListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindEventList(e.PageIndex);
    }

    [WebMethod]
    public static int SaveEventBooking(App.CustomEntity.EventBooking customEventBooking)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        if (context.EventBookings.FirstOrDefault(E => E.EventID == customEventBooking.EventID && String.Compare(E.Email, customEventBooking.Email, true) == 0) == null)
        {
            EventBooking booking = new EventBooking();
            booking.EventID = customEventBooking.EventID;
            booking.Email = customEventBooking.Email;
            booking.Address = customEventBooking.Address;
            booking.ContactNo = customEventBooking.ContactNo;
            booking.Name = customEventBooking.Name;
            booking.Created = DateTime.Now;
            booking.Modified = DateTime.Now;

            context.AddToEventBookings(booking);
            context.SaveChanges();
            return 1;
        }
        else
            return -1;
    }
}